package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-11 
 * @Version: 1.0
 */

public class LC2006差的绝对值为k的数对数目 {

    public int countKDifference(int[] nums, int k) {

        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],map.getOrDefault(nums[i],0) + 1);
        }

        int count = 0;
        for (Integer i : map.keySet()) {
            if (map.containsKey(i+k)){
                count += map.get(i)*map.get(i+k);
            }
        }

        return count;
    }


    @Test
    public void test() {
        // 测试代码

        countKDifference(new int[]{7,7,8,3,1,2,7,2,9,5},6);
    }
}
